//
//  GQHOrderView.m
//  Xanthium
//
//  Created by GuanQinghao on 10/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHOrderView.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"

@interface GQHOrderView () <UITableViewDelegate,UITableViewDataSource,GQHOrderCellDelegate>

@end

@implementation GQHOrderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight + 60);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _qh_orderModelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 185.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    GQHOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[GQHOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.qh_delegate = self;
    cell.qh_oneButton.tag = indexPath.row;
    cell.qh_twoButton.tag = indexPath.row;
    cell.qh_threeButton.tag = indexPath.row;
    cell.qh_fourButton.tag = indexPath.row;
    
    GQHOrderModel *orderModel = _qh_orderModelArray[indexPath.row];
    
    /**
     /// 第一个按钮(查看协议)
     @property (nonatomic, strong) UIButton *qh_oneButton;
     @property (nonatomic, strong) UIView *qh_oneView;
     
     /// 第二个按钮(联系司机)
     @property (nonatomic, strong) UIButton *qh_twoButton;
     @property (nonatomic, strong) UIView *qh_twoView;
     
     /// 第三个按钮(取消订单/在途跟踪)
     @property (nonatomic, strong) UIButton *qh_threeButton;
     @property (nonatomic, strong) UIView *qh_threeView;
     
     /// 第四个按钮(确认装车/确认确认送达/立即评价)
     @property (nonatomic, strong) UIButton *qh_fourButton;
     @property (nonatomic, strong) UIView *qh_fourView;
     */
    
    cell.qh_orderIDLabel.text = orderModel.qh_orderNumber;
    cell.qh_routeLabel.text = [NSString stringWithFormat:@"%@-%@",orderModel.qh_departure,orderModel.qh_destination];
    cell.qh_contentLabel.text = [NSString stringWithFormat:@"%@/%.2f/%@/%@",orderModel.qh_freightType,orderModel.qh_freightWeight,orderModel.qh_modelWanted,orderModel.qh_lengthWanted];
    cell.qh_nameLabel.text = orderModel.qh_driverName;
    cell.qh_intervalLabel.text = orderModel.qh_loadingTime;
    
    /// 按钮重新布局
    CGFloat width = 0.25 * (self.qh_width - GQH_MARGIN_DOUBLE);
    [cell.qh_oneView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(width);
    }];
    [cell.qh_twoView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(width);
    }];
    [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(width);
    }];
    [cell.qh_fourView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(width);
    }];
    
    if ([orderModel.qh_orderState isEqualToString:@"00"]) {
        
        /// 待司机确认装货
        cell.qh_statusLabel.text = @"待装货";
        
        if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
            
            [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
            [cell.qh_twoButton setTitle:@"联系货主" forState:UIControlStateNormal];
            [cell.qh_threeButton setTitle:@"取消订单" forState:UIControlStateNormal];
            [cell.qh_fourButton setTitle:@"确认装车" forState:UIControlStateNormal];
            
            [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
        } else {
            
            [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
            [cell.qh_twoButton setTitle:@"联系司机" forState:UIControlStateNormal];
            [cell.qh_threeButton setTitle:@"取消订单" forState:UIControlStateNormal];
            [cell.qh_fourButton setTitle:@"确认装车" forState:UIControlStateNormal];
            
            [cell.qh_fourView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
        }
    }
    if ([orderModel.qh_orderState isEqualToString:@"01"]) {
        /// 待货主确认装货
        cell.qh_statusLabel.text = @"待装货";
        
        if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
            
            [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
            [cell.qh_twoButton setTitle:@"联系货主" forState:UIControlStateNormal];
            [cell.qh_threeButton setTitle:@"取消订单" forState:UIControlStateNormal];
            [cell.qh_fourButton setTitle:@"确认装车" forState:UIControlStateNormal];
            
            [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
            [cell.qh_fourView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
        } else {
            
            [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
            [cell.qh_twoButton setTitle:@"联系司机" forState:UIControlStateNormal];
            [cell.qh_threeButton setTitle:@"取消订单" forState:UIControlStateNormal];
            [cell.qh_fourButton setTitle:@"确认装车" forState:UIControlStateNormal];
            
            [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
        }
    }
    if ([orderModel.qh_orderState isEqualToString:@"10"]) {
        /// 待司机确认送达
        cell.qh_statusLabel.text = @"运输中";
        if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
            
            [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
            [cell.qh_twoButton setTitle:@"联系货主" forState:UIControlStateNormal];
            [cell.qh_threeButton setTitle:@"取消订单" forState:UIControlStateNormal];
            [cell.qh_fourButton setTitle:@"已卸货" forState:UIControlStateNormal];
            
            [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
        } else {
            
            [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
            [cell.qh_twoButton setTitle:@"联系司机" forState:UIControlStateNormal];
            [cell.qh_threeButton setTitle:@"在途追踪" forState:UIControlStateNormal];
            [cell.qh_fourButton setTitle:@"确认收货" forState:UIControlStateNormal];
            
            [cell.qh_fourView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
        }
    }
    if ([orderModel.qh_orderState isEqualToString:@"11"]) {
        /// 待货主确认送达
        cell.qh_statusLabel.text = @"运输中";
        if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
            
            [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
            [cell.qh_twoButton setTitle:@"联系货主" forState:UIControlStateNormal];
            [cell.qh_threeButton setTitle:@"取消订单" forState:UIControlStateNormal];
            [cell.qh_fourButton setTitle:@"已卸货" forState:UIControlStateNormal];
            
            [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
            [cell.qh_fourView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
        } else {
            
            [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
            [cell.qh_twoButton setTitle:@"联系司机" forState:UIControlStateNormal];
            [cell.qh_threeButton setTitle:@"在途追踪" forState:UIControlStateNormal];
            [cell.qh_fourButton setTitle:@"确认收货" forState:UIControlStateNormal];
        }
    }
    if ((_qh_selectedTag == 104) && [orderModel.qh_orderState isEqualToString:@"20"]) {
        /// 已完成
        cell.qh_statusLabel.text = @"已完成";
        if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
            
            [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
            [cell.qh_twoButton setTitle:@"联系货主" forState:UIControlStateNormal];
            [cell.qh_threeButton setTitle:@"取消订单" forState:UIControlStateNormal];
            
            if ([orderModel.qh_invoiceState isEqualToString:ORDER_STATUS_I0]) {
                
                [cell.qh_fourButton setTitle:@"开发票" forState:UIControlStateNormal];
            } else {
                
                [cell.qh_fourButton setTitle:@"已开票" forState:UIControlStateNormal];
            }
            
            [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
        } else {
            
            [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
            [cell.qh_twoButton setTitle:@"联系司机" forState:UIControlStateNormal];
            [cell.qh_threeButton setTitle:@"在途追踪" forState:UIControlStateNormal];
            [cell.qh_fourButton setTitle:@"代开发票" forState:UIControlStateNormal];
            
            [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
            [cell.qh_fourView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
        }
    }
    if ((_qh_selectedTag == 102) && [orderModel.qh_orderState isEqualToString:@"20"]) {
        /// 待评价
        cell.qh_statusLabel.text = @"待评价";
        if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
            
            if ([orderModel.qh_orderReviewedState isEqualToString:@"02"]) {
                
                [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
                [cell.qh_twoButton setTitle:@"联系货主" forState:UIControlStateNormal];
                [cell.qh_threeButton setTitle:@"取消订单" forState:UIControlStateNormal];
                [cell.qh_fourButton setTitle:@"立即评价" forState:UIControlStateNormal];
                
                [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
                    
                    make.width.mas_equalTo(0);
                }];
            } else {
                [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
                [cell.qh_twoButton setTitle:@"联系货主" forState:UIControlStateNormal];
                [cell.qh_threeButton setTitle:@"取消订单" forState:UIControlStateNormal];
                [cell.qh_fourButton setTitle:@"立即评价" forState:UIControlStateNormal];
                
                [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
                    
                    make.width.mas_equalTo(0);
                }];
                [cell.qh_fourView mas_updateConstraints:^(MASConstraintMaker *make) {
                    
                    make.width.mas_equalTo(0);
                }];
            }
        } else {
            
            if ([orderModel.qh_orderReviewState isEqualToString:@"02"]) {
                
                [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
                [cell.qh_twoButton setTitle:@"联系司机" forState:UIControlStateNormal];
                [cell.qh_threeButton setTitle:@"取消订单" forState:UIControlStateNormal];
                [cell.qh_fourButton setTitle:@"立即评价" forState:UIControlStateNormal];
                
                [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
                    
                    make.width.mas_equalTo(0);
                }];
            } else {
                [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
                [cell.qh_twoButton setTitle:@"联系司机" forState:UIControlStateNormal];
                [cell.qh_threeButton setTitle:@"取消订单" forState:UIControlStateNormal];
                [cell.qh_fourButton setTitle:@"立即评价" forState:UIControlStateNormal];
                
                [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
                    
                    make.width.mas_equalTo(0);
                }];
                [cell.qh_fourView mas_updateConstraints:^(MASConstraintMaker *make) {
                    
                    make.width.mas_equalTo(0);
                }];
            }
        }
    }
    if ([orderModel.qh_orderState isEqualToString:@"30"]) {
        /// 已取消
        cell.qh_statusLabel.text = @"已取消";
        if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
            
            [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
            [cell.qh_twoButton setTitle:@"联系货主" forState:UIControlStateNormal];
            [cell.qh_threeButton setTitle:@"取消订单" forState:UIControlStateNormal];
            [cell.qh_fourButton setTitle:@"代开发票" forState:UIControlStateNormal];
            
            [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
            [cell.qh_fourView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
        } else {
            
            [cell.qh_oneButton setTitle:@"查看协议" forState:UIControlStateNormal];
            [cell.qh_twoButton setTitle:@"联系司机" forState:UIControlStateNormal];
            [cell.qh_threeButton setTitle:@"在途追踪" forState:UIControlStateNormal];
            [cell.qh_fourButton setTitle:@"代开发票" forState:UIControlStateNormal];
            
            [cell.qh_threeView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
            [cell.qh_fourView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.width.mas_equalTo(0);
            }];
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
        
        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)qh_loadedOrUnloadedOrComment:(UIButton *)sender {
    DLog(@"第四个按钮 - 确认装车或确认送达或评价");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_loadedOrUnloadedOrComment:)]) {
        
        [_qh_delegate qh_loadedOrUnloadedOrComment:sender];
    }
}

- (void)qh_cancelOrMap:(UIButton *)sender {
    DLog(@"第三个按钮 - 取消订单或在途追踪");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_cancelOrMap:)]) {
        
        [_qh_delegate qh_cancelOrMap:sender];
    }
}

- (void)qh_checkContract:(UIButton *)sender {
    DLog(@"第一个按钮 - 查看协议内容");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkContract:)]) {
        
        [_qh_delegate qh_checkContract:sender];
    }
}

- (void)qh_dialMobile:(UIButton *)sender {
    DLog(@"第二个按钮 - 拨打电话");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_dialMobile:)]) {
        
        [_qh_delegate qh_dialMobile:sender];
    }
}

@end
